<?php
include("dataprovider.php");
include ("Cart.php");
?>
<?php include("includes/function.php") ?>
<?php include("includes/header.php") ?>
<div class="container">
    <div id="my-post">
        <h1>Chi tiết sản phẩm</h1>
        <div class="best-selling">
            <?php
            $t = $_GET['detailid'];
            $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where b.ProductID = '$t'";
            $r = mysqli_query($dbc, $q);
            confirm_query($r, $q);
            if ($r != false) {
                $row = mysqli_fetch_array($r, MYSQL_ASSOC);
                $count = $row['ProductView'] + 1;
                $qu = "Update Product set ProductView = '$count' where ProductID = '$t'";
                $re = mysqli_query($dbc, $qu);
                confirm_query($re, $qu);
                ?>
                <div id="product-detail">
                    <table>
                        <tr>
                            <td rowspan="10"><img src="<?php echo $row["ProductImg"]; ?>" width="250"/></td>
                        </tr>
                        <tr></tr>
                        <tr>
                            <td class="tieude">Tên sản phẩm:</td>
                            <td class="thongtin"><?php echo $row["ProductName"]; ?></td>

                        </tr>
                        <tr>
                            <td class="tieude">Giá bán:</td>
                            <td class="thongtin"><red><?php echo number_format($row["ProductPrice"]) ?> VNĐ</red></td>
                        </tr>
                        <tr>
                            <td class="tieude">Số lượt xem:</td>
                            <td class="thongtin"><?php echo $row["ProductView"]; ?></td>
                        </tr>
                        <tr>
                            <td class="tieude">Số lượng bán:</td>
                            <td class="thongtin"><?php echo $row["ProductSell"]; ?></td>
                        </tr>
                        <tr>
                            <td class="tieude">Mô tả:</td>
                            <td class="thongtin"><?php echo $row["ProductDesc"]; ?></td>
                        </tr>
                        <tr>
                            <td class="tieude">Xuất xứ:</td>
                            <td class="thongtin"><?php echo $row["OriginName"]; ?></td>
                        </tr>
                        <tr>
                            <td class="tieude">Nhà sản xuất:</td>
                            <td class="thongtin"><?php echo $row["ManuName"]; ?></td>
                        </tr>
                        <tr><td></td>
                            <td><form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post"
                                      name="form<?php echo $row['ProductID']; ?>" width="440px">

                                    <span>
                                        <input name="productid<?php echo $row['ProductID']; ?>" type="hidden" 
                                               value="<?php echo $row['ProductID']; ?>" />
                                        <input name="productname<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo $row['ProductName']; ?>" />
                                        <input name="productprice<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo $row['ProductPrice']; ?>" />
                                        <input name="quantity<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo '1'; ?>" />
                                        <input name="submit<?php echo $row['ProductID']; ?>" type="submit"  value = 
                                               "Thêm vào giỏ hàng" onclick="<?php
                                               if (isset($_SESSION['cartname']) == false) {
                                                   echo "alert('Bạn cần phải đăng nhập để mua hàng');";
                                               }
                                               if (isset($_REQUEST["productid" . $row['ProductID']]) && isset($_SESSION["cartname"]) == true) {
                                                   $_SESSION["cartname"]->InsertProduct($_REQUEST["productid" . $row['ProductID']], $_REQUEST["productname" . $row['ProductID']], $_REQUEST["quantity" . $row['ProductID']], $_REQUEST["productprice" . $row['ProductID']]);
                                                   echo "alert('Sản phẩm trong giỏ hàng: " . $_SESSION['cartname']->SumProduct() . " sản phẩm')";
                                               }}
                                               ?>"/></span>
                                </form></td></tr>         
                    </table>
                </div>
            </div>
        </div>
        
        <div id="my-post">
        <h1>Sản phẩm cùng loại</h1>
        <div class="best-selling">
            <?php
            $q = "select * from Product b where b.ProductID = '$t'";
            $r = mysqli_query($dbc, $q);
            confirm_query($r, $q);
            $row = mysqli_fetch_array($r, MYSQL_ASSOC);
            $c = $row['ProductCat'];
            $q = "select b.*, c.ManuName, d.OriginName from ((product b inner join origin d on d.OriginID = b.ProductOriID) 
inner join manufacturer c on c.ManuID = b.ProductManuID) where b.ProductCat = '$c' and b.ProductID <> '$t'";
            $r = mysqli_query($dbc, $q);
            confirm_query($r, $q);
            if ($r != false) {
                ?>
                <div id="product">
                    <ul>
                        <?php
                        $i = 0;
                        while ($row = mysqli_fetch_array($r, MYSQL_ASSOC)) {
                            $i += 1;
                            if($i > 5){
                                Break;
                            }
                            ?>
                            <li>
                                <a href="productdetail.php?detailid=<?php echo $row['ProductID'] ?>">
                                    <img src="<?php echo $row["ProductImg"]; ?>" width="150" height="150" alt="" />
                                    <p><?php echo $row["ProductName"]; ?></br>Giá: <red><?php echo number_format($row["ProductPrice"]) ?> VNĐ</red> </p></a>

                                <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post"
                                      name="form<?php echo $row['ProductID']; ?>" width="440px">

                                    <span>
                                        <input name="productid<?php echo $row['ProductID']; ?>" type="hidden" 
                                               value="<?php echo $row['ProductID']; ?>" />
                                        <input name="productname<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo $row['ProductName']; ?>" />
                                        <input name="productprice<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo $row['ProductPrice']; ?>" />
                                        <input name="quantity<?php echo $row['ProductID']; ?>" type="hidden"
                                               value="<?php echo '1'; ?>" />
                                        <input name="submit<?php echo $row['ProductID']; ?>" type="submit"  value = 
                                               "Thêm vào giỏ hàng" onclick="<?php
                                               if (isset($_SESSION['cartname']) == false) {
                                                   echo "alert('Bạn cần phải đăng nhập để mua hàng');";
                                               }
                                               if (isset($_REQUEST["productid" . $row['ProductID']]) && isset($_SESSION["cartname"]) == true) {
                                                   $_SESSION["cartname"]->InsertProduct($_REQUEST["productid" . $row['ProductID']], $_REQUEST["productname" . $row['ProductID']], $_REQUEST["quantity" . $row['ProductID']], $_REQUEST["productprice" . $row['ProductID']]);
                                                   echo "alert('Sản phẩm trong giỏ hàng: " . $_SESSION['cartname']->SumProduct() . " sản phẩm')";
                                               }
                                               ?>"/></span>
                                </form>
                            </li>
                        <?php 
                            }}
                        ?>
                    </ul>
                </div>
        </div>
    </div>
</div>
    <?php include("includes/footer.php") ?>